package com.yx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.entity.User;
import com.yx.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yx
 * @since 2022-02-07
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService us;
    @GetMapping("/selectAll")
    public List<User> selectAll(){
        return us.list();
    }
    @PostMapping("/add")
    public void add(@RequestBody User user){
        user.setState(1);
        us.save(user);
    }
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") int id){
        us.removeById(id);
    }
    @GetMapping("/login/{username}/{password}/{role}")
    public boolean login(@PathVariable("username") String username,
                         @PathVariable("password") String password,
                         @PathVariable("role") int role,
                         HttpSession session){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("email",username);
        wrapper.eq("password",password);
        Object one=null;
        if(role==1){
            one = us.getOne(wrapper);
            session.setAttribute("user",one);
        }
        if (role==2){
            if("admin".equals(username)&&"admin".equals(password)){
                return true;
            }
        }

        return one!=null;
    }
}

